#include <iostream>
#include <vector>
#include <map>
using namespace std;

// 100.00 %
class Solution
{
  public:
	bool wordPattern(string pattern, string str)
	{
		map<char, string> myMap;
		map<string, char> myMap2;
		vector<string> myVec = splitWithStl(str, " ");
		if (pattern.size() != myVec.size())
			return false;
		for (int i = 0; i < pattern.size(); i++)
		{
			if (myMap.find(pattern[i]) == myMap.end())
				myMap[pattern[i]] = myVec[i];
			else if (myMap[pattern[i]] != myVec[i])
				return false;
		}
		for (int i = 0; i < myVec.size(); i++)
		{
			if (myMap2.find(myVec[i]) == myMap2.end())
				myMap2[myVec[i]] = pattern[i];
			else if (myMap2[myVec[i]] != pattern[i])
				return false;
		}
		return true;
	}
	std::vector<std::string> splitWithStl(const std::string &str, const std::string &pattern)
	{
		std::vector<std::string> resVec;

		if ("" == str)
		{
			return resVec;
		}

		std::string strs = str + pattern;

		size_t pos = strs.find(pattern);
		size_t size = strs.size();

		while (pos != std::string::npos)
		{
			std::string x = strs.substr(0, pos);
			resVec.push_back(x);
			strs = strs.substr(pos + 1, size);
			pos = strs.find(pattern);
		}

		return resVec;
	}
};

int main()
{
	string pattern = "abbc", str = "dog cat";
	cout << Solution().wordPattern(pattern, str) << endl;
	return 0;
}
